/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function $http(sMethod, sUrl)
{
	var oHttp = new ActiveXObject("Microsoft.XMLHTTP");
	oHttp.open(sMethod,
			   sUrl,
			   false);
	oHttp.setRequestHeader("Content-Type",
						   "application/x-www-form-urlencoded");
	return oHttp;
}



function $get(sElementId)
{
	return document.getElementById(sElementId);
};


function $create(sElementTag)
{
	return document.createElement(sElementTag);
};


function $createDelegate(oContext, oMethod)
{
	return function() {	return oMethod.apply(oContext, arguments); };
};


function $createEvent()
{
	return document.createEventObject();
};


Date.prototype.format = function()
{
	var nMinutes = this.getMinutes();
	var nSeconds = this .getSeconds();
	var nMilliseconds = this.getMilliseconds();
	nMinutes = (nMinutes < 10) ? "0" + nMinutes : nMinutes.toString();
	nSeconds = (nSeconds < 10) ? "0" + nSeconds : nSeconds.toString();
	nMilliseconds = (nMilliseconds < 10) ? "0" + nMilliseconds : nMilliseconds.toString();
	return nMinutes + ":" + nSeconds + "." + nMilliseconds;
};


Date.prototype.toDisplay = function()
{
	var aMonths = ["January",
				   "February",
				   "March",
				   "April",
				   "May",
				   "June",
				   "July",
				   "August",
				   "September",
				   "October",
				   "November",
				   "December"];
	var nMonth = this.getMonth();
	var nDate = this.getDate();
	var nYear = this.getFullYear();
	
	return aMonths[nMonth] + "&nbsp;" + ((10 > nDate) ? "&nbsp;" + nDate : nDate) + ",&nbsp;" + nYear;
};


Date.prototype.toTimeDisplay = function()
{
	var nHour = this.getHours();
	var nMinute = this.getMinutes();
	var sAMPM = (12 > nHour) ? "AM" : "PM";
	
	if (12 <= nHour)
	{
		nHour -= 12;
	}
	if (0 == nHour)
	{
		nHour = 12;
	}
	
	if (10 > nHour)
	{
		nHour = "&nbsp;" + nHour;
	}

	if (10 > nMinute)
	{
		nMinute = "0" + nMinute;
	}
	


	
	return nHour + ":" + nMinute + "&nbsp;" + sAMPM;
};
	

String.prototype.trim = function() 
{ 
	return this.replace(/^\s+|\s+$/g, ''); 
};

String.prototype.nbsp = function()
{
	return this.replace(/\s+/g, "&nbsp;");
};

function $getQueryString(sName)
{
	sName = sName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var sRegex = "[\\?&]" + sName + "=([^&#]*)";
	var rexName = new RegExp(sRegex);
	var oResults = rexName.exec(window.location.href);
	if (null == oResults)
	{
		return null;
	}
	else
	{
		return decodeURI(oResults[1]);
	}
}

function BodyLoad()
{		
	window.oMenu = new BVMenu();


	WindowResizer.resize();
	window.attachEvent("onresize",
					   WindowResizer.resize);

	window.g_bIgnoreEvents = false;
	window.nNextPopup = 0;
	window.oBrowsers = [];


	window.oToolbar = new BVToolbar();

	window.oPopupScripts = {};
	$get("objMainBrowser").BrowserID = "objMainBrowser";
	window.objActiveBrowser = $get("objMainBrowser");
	window.oAppEvents = new BVAppEvents("objMainBrowser");
	window.oPopupScripts["objMainBrowser"] = window.oAppEvents;


	window.oReplay = new BVReplay();
	window.oReport = new BVReport();
	window.oPopupTabs = new BVPopupTabs();
	window.oActionTree = new BVActionTree();
	window.oLogin = new BVLogin();
	window.oProfiles = new Profiles();
	window.oScreenshots = new BVScreenshots();

	window.oMenu.init();
	window.oProfiles.init();
	



	if (null != $getQueryString("nav"))
	{
		sURL = $getQueryString("nav");
		window.oAppEvents.ignoreWelcomeMessage = true;
		$get("txtToolbarAddress").value = sURL;
	}

	if (null != $getQueryString("script"))
	{
		oAppEvents.OpenScript($getQueryString("script"));
	}


	if ("undefined" != typeof(window.loginMessage) &&
		"" != window.loginMessage)
	{
		window.oLogin.userLoggedIn(window.loginMessage);
	}
	else
	{
		window.oLogin.noUser();
	}
	if ("run" === $getQueryString("replay"))
	{
		window.oReplay.replay();
	}

}
